package com.server;

import com.model.*;

import java.util.*;

import java.io.*;

/**
 * 服务器主功能
 */
public class ServerView {
    private ServerInitClose serverInitClose;
    private ServerDao serverDao;
    private boolean isManagerLogin;
    private boolean isStudentLogin;


    public ServerView(ServerInitClose serverInitClose, ServerDao serverDao) {
        this.serverInitClose = serverInitClose;
        this.serverDao = serverDao;
    }


    /**
     * 服务器接收客户端发来的请求对象，返回该对象
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void serverReceive() throws IOException, ClassNotFoundException{
        while (true){
            // 服务端接收客户端发来的对象
            UserMessage userMessage = (UserMessage) serverInitClose.getObjectInputStream().readObject();
            System.out.println("接收到的消息是：" + userMessage.toString());
            // 若接收到的是管理员登陆
            if ("managerCheck".equals(userMessage.getMessage())){
                // 判断管理员账号和密码是否正确
                isManagerLogin = judgeManager(userMessage);
                // 若正确，则进入管理员系统
                while (isManagerLogin){
                    // 接收客户端的操作选项
                    Integer receiveOption = (Integer) serverInitClose.getObjectInputStream().readObject();
                    if (receiveOption == 0)
                        break;
                    while (true){
                        if (receiveOption == 1){
                            // 进入服务端学员管理模块
                            boolean isManagerQuit = this.serverReceiveStudentSystem(new StudentSystemOperation());
                            if (isManagerQuit)
                                break;
                        }
                        else
                            break;
                    }
                    while (true){
                        if (receiveOption == 2){
                            // 进入考题管理模块
                            boolean isExamQuit = this.serverReceiveExamSystem(new ExamSystemOperation());
                            if (isExamQuit)
                                break;
                        }
                        else
                            break;
                    }
                }
            }
            else if ("userCheck".equals(userMessage.getMessage())){
                // 判断学员账号和密码是否正确
                isStudentLogin = judgeStudent(userMessage);
                // 若正确进入学员系统
                while (isStudentLogin){
                    // 接收客户端操作选项
                    Integer receiveOption = (Integer) serverInitClose.getObjectInputStream().readObject();
                    if (receiveOption == 0)
                        break;
                    while (true){
                        if (receiveOption == 1){
                            // 进入服务端用户模块
                            boolean isStudentQuit = this.serverReceiveStudentSubSystem(new StudentSystemOperation());
                            if (isStudentQuit)
                                break;
                        }else
                            break;
                    }
                    while (true){
                        if (receiveOption == 2){
                            // 进入服务端考试模块
                            boolean isExamQuit = this.serverReceiveExamSubSystem(new ExamSystemOperation());
                            if (isExamQuit)
                                break;
                        }else
                            break;
                    }
                }
            }
            else
                break;
        }
    }

    /**
     * 服务器对接收到的对象进行判断处理
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean judgeManager(UserMessage userMessage) throws IOException{
        boolean res;
        if (serverDao.serverManagerCheck(userMessage.getUser())){
            userMessage.setMessage("success");
            //serverInitClose.getObjectOutputStream().writeObject(userMessage);
            System.out.println("服务器发送连接成功消息！");
            //serverSendStudentFile();
            res = true;
        }
        else{
            userMessage.setMessage("fail");
            System.out.println("服务器发送连接失败消息！");
            //serverInitClose.getObjectOutputStream().writeObject(userMessage);
            res = false;
        }
        serverInitClose.getObjectOutputStream().writeObject(userMessage);
        return res;
    }

    /**
     * 服务器对接收到的对象进行判断处理
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean judgeStudent(UserMessage userMessage) throws IOException{
        if (serverDao.serverStudentCheck(userMessage.getUser())){
            userMessage.setMessage("success");
            serverInitClose.getObjectOutputStream().writeObject(userMessage);
            System.out.println("服务器发送连接成功消息！");
            return true;
        }
        else{
            userMessage.setMessage("fail");
            System.out.println("服务器发送连接失败消息！");
            serverInitClose.getObjectOutputStream().writeObject(userMessage);
            return false;
        }
    }

    /**
     * 服务器接收客户端发来的学员系统请求，并进行操作
     */
    public boolean serverReceiveStudentSystem(StudentSystemOperation studentSystemOperation) throws IOException, ClassNotFoundException {
        List<Student> sendStudents = new ArrayList<>();
        StudentMessage receiveStudentMessage = (StudentMessage)serverInitClose.getObjectInputStream().readObject();
        boolean isOption = false;
        boolean isQuit = false;
        switch (receiveStudentMessage.getMessage()){
            case "add":
                isOption = studentSystemOperation.add(receiveStudentMessage.getStudent());
                break;
            case "delete":
                isOption = studentSystemOperation.delete(receiveStudentMessage.getStudent().getId());
                break;
            case "modify":
                isOption = studentSystemOperation.modify(receiveStudentMessage.getStudent());
                break;
            case "find":
                Student findStudent = studentSystemOperation.find(receiveStudentMessage.getStudent().getId());
                if (findStudent != null){
                    receiveStudentMessage.setStudent(findStudent);
                    isOption = true;
                }
                break;
            case "show":
                sendStudents = studentSystemOperation.showAll();
                if (sendStudents.size() != 0 || sendStudents != null){
                    receiveStudentMessage.setStudents(sendStudents);
                    isOption = true;
                }
                break;
            case "quit":
                isQuit = true;
                return isQuit;
            default:
                isOption = false;
        }
        // 判断是否操作成功
        if (isOption)
            receiveStudentMessage.setMessage("success");
        else
            receiveStudentMessage.setMessage("fail");
        // 返回给客户端
        serverInitClose.getObjectOutputStream().writeObject(receiveStudentMessage);
        return isQuit;
    }

    /**
     * 服务器接收客户端发来的考试系统请求，并进行操作
     * @param examSystemOperation
     */
    public boolean serverReceiveExamSystem(ExamSystemOperation examSystemOperation) throws IOException, ClassNotFoundException {
        ExamMessage receiveExamMessage = (ExamMessage)serverInitClose.getObjectInputStream().readObject();
        boolean isOption = false;
        boolean isQuit = false;
        switch (receiveExamMessage.getMessage()){
            case "add":
                isOption = examSystemOperation.add(receiveExamMessage.getExam());
                break;
            case "delete":
                isOption = examSystemOperation.delete(receiveExamMessage.getExam().getId());
                break;
            case "modify":
                isOption = examSystemOperation.modify(receiveExamMessage.getExam());
                break;
            case "find":
                Exam findExam = examSystemOperation.find(receiveExamMessage.getExam().getQuestion());
                if (findExam != null){
                    receiveExamMessage.setExam(findExam);
                    isOption = true;
                }
                break;
            case "import":
                isOption = examSystemOperation.importExam(receiveExamMessage.getImportFile());
                break;
            case "quit":
                isQuit = true;
                return isQuit;
            default:
                isOption = false;
        }

        if (isOption)
            receiveExamMessage.setMessage("success");
        else
            receiveExamMessage.setMessage("fail");
        serverInitClose.getObjectOutputStream().writeObject(receiveExamMessage);
        return isQuit;
    }

    /**
     * 服务器端接收来自客户端学员子系统的请求
     */
    public boolean serverReceiveStudentSubSystem(StudentSystemOperation studentSystemOperation) throws IOException, ClassNotFoundException {
        // 接收客户端发来的对象，是一个UserMessage对象
        UserMessage receiveUserMessage = (UserMessage) serverInitClose.getObjectInputStream().readObject();
        // 判断是否为修改密码操作
        if (receiveUserMessage.getMessage().equals("modifyPassWord")){
            boolean isModify = studentSystemOperation.modify(receiveUserMessage.getUser());
            if (isModify){
                receiveUserMessage.setMessage("successModifyPassWord");
            }else{
                receiveUserMessage.setMessage("failModifyPassWord");
            }
            // 发送回客户端
            serverInitClose.getObjectOutputStream().writeObject(receiveUserMessage);
            return false;
        }
        // 退出系统
        else{
            return true;
        }
    }

    /**
     * 服务器端接收来自客户端考试子系统的请求
     */
    public boolean serverReceiveExamSubSystem(ExamSystemOperation examSystemOperation) throws IOException, ClassNotFoundException {

        UserMessage receiveUserMessage = (UserMessage) serverInitClose.getObjectInputStream().readObject();
        StudentMessage sendStudentMessage = null;
        String option = receiveUserMessage.getMessage();
        boolean res = false;

        switch (option){
            case "startExam":
                sendStudentMessage = examSystemOperation.sendExamOrScore(receiveUserMessage);
                // 发送给客户端
                serverInitClose.getObjectOutputStream().writeObject(sendStudentMessage);
                // 接收客户端传来的score
                Integer score = (Integer) serverInitClose.getObjectInputStream().readObject();
                examSystemOperation.serverModifyScore(sendStudentMessage,score);
                break;
            case "queryScore":
                sendStudentMessage = examSystemOperation.sendExamOrScore(receiveUserMessage);
                // 发送给客户端
                serverInitClose.getObjectOutputStream().writeObject(sendStudentMessage);
                break;
            case "ExportScore":
                sendStudentMessage = examSystemOperation.sendExamOrScore(receiveUserMessage);
                // 发送给客户端
                serverInitClose.getObjectOutputStream().writeObject(sendStudentMessage);
                break;
            case "quit":
                res = true;
                return res;
            default:
                break;
        }
        return res;
    }
}
